<?
class InputMultiple extends Input
{
	var $oInput;
	var $cValueSeparator = ''; // Separa Valor de la descripcion (InputLookUp)
	var $cSeparator = ',';


	function setWidth( $cWidth )
	{
		if ( $this->oInput ) $this->oInput->setWidth( $cWidth );
		$this->cWidth = $cWidth;
	}

	function setValue( $cValue )
		{
			$cClassInput = get_class($this->oInput) ;
			if ( $cClassInput == "InputLookUp"  || $cClassInput == "InputArray"  ) 	
			{
				$aValues = explode( $this->cSeparator,  $cValue);
				foreach( $aValues as $cValue ) 
				{
					$this->addOption( $cValue , $this->oInput->getDescription( $cValue ) ) ;
				}
			} else $this->cValue= $cValue;
		}

	function addOption( $cValue, $cDescription )
	{
		if (    $cDescription  &&  !($this->cValueSeparator=== '' ) ) 
		{
			$this->cValue .=  $cValue . $this->cValueSeparator .  $cDescription . $this->cSeparator;
		}
		elseif ( $cValue  )
			$this->cValue .=  $cValue . $this->cSeparator;
	}
	function changedValues()
		{
			return getParam($this->cField . "_cbo");
		}
	function setValueSeparator( $cValueSeparator )
		{
			$this->cValueSeparator = $cValueSeparator;
		}
	function setSeparator( $cSeparator )
		{
			$this->cSeparator = $cSeparator;
		}
	function InputMultiple( $cLabel, $cField, $cValue = '', $bMandatory = false)
	{
		$this->Input( $cLabel, $cField, $cValue, $bMandatory);
		if ( getParam($this->cField . "_val") || getParam($this->cField . "_txt") )
		{
			$this->cValue = getParam($this->cField . "_val");
			if ( $this->cValue == '' && getParam($this->cField . "_txt") != '' ) $this->cValue = getParam($this->cField . "_txt");
		}
	}
	  function setForm( &$oForm )
		{
		  parent::setForm( $oForm );
		  $this->oInput->setForm( $oForm );
		}
	function make()
		{
			$cFunctionInsert = "insertMultiple ( '" . $this->getFormName() . "' , '" . $this->cField . "', '" . $this->cSeparator . "' ) " ;
			if ( get_class($this->oInput) == "InputLookUp" ) 	$cFunctionInsert = "insertMultipleLookUp ( '" . $this->getFormName() . "' , '" . $this->cField . "', '" . $this->cSeparator . "' ) " ;
			if ( get_class($this->oInput) == "InputArray" ) 	$cFunctionInsert = "insertMultipleArray ( '" . $this->getFormName() . "' , '" . $this->cField . "', '" . $this->cSeparator . "', '" . $this->oInput->cSeparator . "' ) " ;
			
			if ( $this->cWidth == '') $this->cWidth = "150";
			$cWidth = "style='width: " . $this->cWidth . "px'";
			if ( $this->isHidden() ) return parent::make();
			$cReturn = "<div style='float: left;'>";
			$this->oInput->setField( $this->cField . "_txt" );
			$cReturn .=  $this->oInput->make() ;
			$cReturn .= $this->oInput->showActions();
			$cReturn .= "<a href='javascript:void(0);' onclick=\"$cFunctionInsert ; return false;\" >Agregar</a><br/>";
			$cReturn .= "<select id='" . $this->cField . "_lst' name='" . $this->cField . "_lst' multiple='true' $cWidth  size='3' >";
			if ( $this->cValue )
			{
				$aValues = explode( $this->cSeparator , $this->cValue );
				if ( $this->cValueSeparator )
				{
					$cHiddenValue = '';
					foreach( $aValues as $cValue )
					{
						if ( $cValue )
						{
							list( $cKey, $cDescripcion ) = explode( $this->cValueSeparator , $cValue );
							$cReturn .= "<OPTION value='$cKey'>$cDescripcion</OPTION>";
							$cHiddenValue .= $cKey . $this->cSeparator;
						}
					}
					$cHiddenValue = substr($cHiddenValue, 0, -strlen($this->cSeparator) );
				} else
				{
					$cHiddenValue  = $this->cValue;
					foreach( $aValues as $cValue )
					{
						if ( $cValue )
						{
							$cReturn .= "<OPTION value='$cValue'>$cValue</OPTION>";
						}
					}
				}
			}
			$cReturn .= "<input  type='hidden' id='" . $this->cField . "_cbo' name='" . $this->cField . "_cbo' value='0' />";
			$cReturn .= "<input  type='hidden' id='" . $this->cField . "_val' name='" . $this->cField . "_val' value='" . $cHiddenValue . "' />";
			$cReturn .= "</select><a href='javascript:void(0);' onclick=\"removeMultiple( '" . $this->getFormName() . "' , '" . $this->cField . "', '" . $this->cSeparator . "' ); return false;\" >Eliminar</a>";
			$cReturn .="</div>";
			return  $cReturn;
		}
	function setInput( &$oInput  )
		{
			$this->oInput = $oInput;
		}
}
?>